﻿@using IotClientApp.Shared
@using IotClientApp.Shared.Enums;
@using Newtonsoft.Json;
@using PluginInterface;
@inject HttpClient httpClient
@inject ISnackbar Snackbar

<MudDialog>
    <TitleContent>
        <MudText Typo="Typo.h6">
            <MudIcon Icon="@Icons.Material.Filled.Edit" Class="mr-3 mb-n1" />
            @Title
        </MudText>
    </TitleContent>
    <DialogContent>
        <MudHidden Value="@pointInfo.Id"></MudHidden>
        <MudTextField @bind-Value="pointInfo.Name" Label="名称" Class="mr-3 mb-n1" />
        <MudTextField @bind-Value="pointInfo.Address" Label="地址" />
        <MudSelect @bind-Value="pointInfo.DataType" Label="类型" AnchorOrigin="Origin.BottomCenter">
            @foreach (var item in DataTypeEnumDic)
            {
                <MudSelectItem Value="@item.Key">@item.Value</MudSelectItem>
            }
        </MudSelect>
        <MudSelect @bind-Value="pointInfo.EndianType" Label="大小端" AnchorOrigin="Origin.BottomCenter">
            @foreach (var item in EndianTypeEnumDic)
            {
                <MudSelectItem Value="@item.Key">@item.Value</MudSelectItem>
            }
        </MudSelect>
        <MudSelect @bind-Value="pointInfo.ReadWrite" Label="权限" AnchorOrigin="Origin.BottomCenter">
            @foreach (var item in ReadWriteEnumDic)
            {
                <MudSelectItem Value="@item.Key">@item.Value</MudSelectItem>
            }
        </MudSelect>
        <MudTextField @bind-Value="pointInfo.Expressions" Label="表达式" />
        <MudTextField @bind-Value="pointInfo.Desc" Label="描述" />

        <div class="d-flex mud-width-full align-center mt-8">
            <MudText Typo="Typo.subtitle1" Class="mr-2">Selected values: </MudText>
                <MudChip Color="Color.Primary">@pointInfo.DataType</MudChip>
                 <MudChip Color="Color.Primary">@pointInfo.EndianType</MudChip>
        </div>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel">取消</MudButton>
        <MudButton Color="Color.Success" OnClick="SaveServer">保存</MudButton>
    </DialogActions>
</MudDialog>
@code {
    private Dictionary<short, string> DataTypeEnumDic = new Dictionary<short, string>();
    private Dictionary<short, string> EndianTypeEnumDic = new Dictionary<short, string>();
    private Dictionary<string, string> ReadWriteEnumDic = new Dictionary<string, string>();
    protected override void OnInitialized()
    {
        foreach (DataTypeEnum item in Enum.GetValues(typeof(DataTypeEnum)))
        {
            DataTypeEnumDic.Add((short)item.GetHashCode(), item.ToString());
        }
        foreach (EndianEnum item in Enum.GetValues(typeof(EndianEnum)))
        {
            EndianTypeEnumDic.Add((short)item.GetHashCode(), item.ToString());
        }
        ReadWriteEnumDic.Add("Read", "可读");
        ReadWriteEnumDic.Add("ReadWrite", "可读可写");
    }
    [CascadingParameter] MudDialogInstance MudDialog { get; set; }

    [Parameter] public DeviceParamModel pointInfo { get; set; } = new DeviceParamModel();
    /// <summary>
    /// 标题
    /// </summary>
    [Parameter]
    public string Title { get; set; } = "新增";

    private DataTypeEnum dataType { get; set; }

    private void Cancel()
    {
        MudDialog.Cancel();
    }
    private void onchange()
    {
        Console.WriteLine();
    }
    private async Task SaveServer()
    {
        var response = await httpClient.PostAsJsonAsync("api/Param/SavePointInfo", new SavePointInfoModel
            {
                Id = pointInfo.Id,
                Name = pointInfo.Name,
                Address = pointInfo.Address,
                DataType = pointInfo.DataType,
                EndianType = pointInfo.EndianType,
                Expressions = pointInfo.Expressions,
                Desc = pointInfo.Desc,
                ReadWrite = pointInfo.ReadWrite,
                DeviceId = pointInfo.DeviceId
            });
        if (response.IsSuccessStatusCode)
        {
            var responseContent = await response.Content.ReadAsStringAsync();
            try
            {
                var result = JsonConvert.DeserializeObject<ApiResult<SavePointInfoModel>>(responseContent);
                if (result != null && result.Code == ResultCode.Success)
                {
                    //In a real world scenario this bool would probably be a service to delete the item from api/database
                    Snackbar.Add(Title, Severity.Success);
                    MudDialog.Close(DialogResult.Ok(pointInfo.Id));
                }
                else
                {
                    Snackbar.Add(result.ErrorMsg, Severity.Error);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        else
        {
            Snackbar.Add(Title, Severity.Error);
        }
    }
}